package com.dagger.weapon.controller;

import com.dagger.weapon.utils.UploadImg;
import com.dagger.weapon.vo.ImageVO;
import com.dagger.weapon.vo.ResStatus;
import com.dagger.weapon.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/images")
@Api(value = "图片上传到tomcat",tags = "图片管理")
@CrossOrigin
public class ImagesController {

    @Autowired
    private UploadImg uploadImg;


    @ApiOperation("上传图片接口")
    @ApiImplicitParams({
            @ApiImplicitParam(dataType = "MultipartFile",name = "image",value = "图片文件",required = true)
    })
    @PostMapping("/upload")
    public ResultVO uploadImg(MultipartFile image){
        String imagesPath = uploadImg.upload(image);
        Object imageUrl= new ImageVO(imagesPath);
        if (imagesPath!=null){
            return new ResultVO(ResStatus.OK,"success",imageUrl);
        }else {
            return new ResultVO(ResStatus.NO,"fail",null);
        }
    }
}
